package com.ordersystem.controller;

import com.ordersystem.domain.Result;
import com.ordersystem.utils.PageUtils;
import com.ordersystem.utils.UploadFileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : CommonController
 * @description : [工具接口类]
 * @createTime : [2022/9/29 11:57]
 * @updateUser : asus1020
 * @updateTime : [2022/9/29 11:57]
 * @updateRemark : [描述说明本次修改内容]
 */
@RestController
@RequestMapping("/common")
@Slf4j
@CrossOrigin
public class CommonController {
    @Resource
    private PageUtils pageUtils;
    @Resource
    UploadFileUtils uploadFileUtils;
    @Value("${imageUrl}")
    private String imageUrl;
    private RequestDispatcher requestDispatcher;

    //上传文件
    @RequestMapping("/uploadFile")
    public Result uploadFile(MultipartFile uploadFile) throws IOException {
        log.info("上传文件名："+uploadFile.getOriginalFilename());
        InputStream headerImgInputStream=null;
        Result result = new Result();
        if (uploadFile.isEmpty()) {
            result.setMessage("上传的文件为空");
            result.setStatus(500);
        } else {
            try {
                headerImgInputStream = uploadFile.getInputStream();
                //分配一个随机文件名
                String visitName = pageUtils.randomFileName(uploadFile.getOriginalFilename());
                //上传文件，返回访问路径
                String realFileUrl = uploadFileUtils.uploadImg(headerImgInputStream, visitName);
                result.setStatus(200);
                result.setData(realFileUrl);
            } catch (IOException e) {
                result.setStatus(500);
                System.out.println(e.getMessage());
                result.setMessage("文件上传失败");
                e.printStackTrace();
            }finally {
                headerImgInputStream.close();
            }
        }
        return result;
    }
    //用户头像回显
    @RequestMapping("/exhibit/{url}")
    public void fileExhibit(@PathVariable("url") String url, HttpServletResponse response){
        String realUrl=imageUrl+url;
        System.out.println(realUrl);
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(realUrl));
            ServletOutputStream responseOutputStream = response.getOutputStream();
            byte[]lens=new byte[1024];
            int len=0;
            while ((len=fileInputStream.read(lens,0,lens.length))!=-1){
                responseOutputStream.write(lens,0,len);
            }
            responseOutputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/{url}")
    public void skipIndex(HttpServletRequest request, HttpServletResponse response, @PathVariable String url) throws IOException {
        response.sendRedirect(request.getContextPath()+"/"+url+"/index.html");
    }
}
